#include "tablemodel.h"

TableModel::TableModel(QObject *parent)
	: QStyledItemDelegate(parent)
{
	m_read.load( ":/res/user-invisible.png" );
	m_unread.load( ":/res/user-online.png" );
}

QSize TableModel::sizeHint(const QStyleOptionViewItem &option,
						   const QModelIndex &index) const
{
	if( index.column() == 1 ){
		QSize size = QStyledItemDelegate::sizeHint(option, index);
		size.setWidth(16);
		size.setHeight( qMax(size.height(), 30) );
		return size;
	}

	return QStyledItemDelegate::sizeHint(option, index);
}

void TableModel::paint(QPainter *painter,
					   const QStyleOptionViewItem &option,
					   const QModelIndex &index) const
{
	QStyledItemDelegate::paint(painter, option, index);

	if( index.column() == 1
		&& qVariantCanConvert<bool>( index.data(Qt::UserRole) ) )
	{
		QRect r = option.rect;
		if( index.data( Qt::UserRole ).toBool() ){
			r.setX( r.x() + (r.width() - m_read.width())/2 );
			r.setY( r.y() + (r.height() - m_read.height())/2 );
			r.setSize( m_read.size() );
			painter->drawPixmap( r, m_read );
		}else{
			r.setX( r.x() + (r.width() - m_unread.width())/2 );
			r.setY( r.y() +(r.height() - m_unread.height())/2 );
			r.setSize( m_unread.size() );
			painter->drawPixmap( r, m_unread );
		}
	}


}
